package one.stand.service.impl;

import com.jmp.base.LabelApi;
import one.stand.common.cache.PublicBaseCacheModel;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.Company2Dto;
import one.stand.dto.CompanyDto;
import one.stand.dto.CompanyServiceDto;
import one.stand.dto.UserDto;
import one.stand.mapper.CompanyMapperExt;
import one.stand.mapper.CompanyPublicMapper;
import one.stand.mapper.CustomerLabelMapperExt;
import one.stand.model.Company;
import one.stand.service.CompanyBaseService;
import one.stand.service.user.UserAdminService;
import one.stand.util.DateUtil;
import one.stand.util.RedisUtil;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author cjr
 */
@Service
@Slf4j
public class CompanyBaseServiceImpl implements CompanyBaseService {
    /**
     * 新用户时间判定
     */
    public static Date DATE_NEW_USER;

    static {
        try {
            DATE_NEW_USER = DateUtil.format.parse("2020-03-01");
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
    }

    @Autowired
    private CompanyMapperExt companyMapperExt;
    @Autowired
    private CustomerLabelMapperExt customerLabelMapperExt;
    @Autowired
    private CompanyPublicMapper companyPublicMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserAdminService userAdminService;

    @Override
    public int audit(Integer id, Integer state, String memo) {
        Company model = new Company();
        model.setCompanyId(id);
        model.setAuditState(state);
        model.setAuditRemark(memo);
        return companyMapperExt.updateByPrimaryKeySelective(model);
    }

    @Override
    public void agent(Integer id, Integer agentMoney, Integer isAgent, Integer companyType) {
        Company model = new Company();
        model.setCompanyId(id);
        model.setAgentMoney(agentMoney);
        model.setIsAgent(isAgent);
        model.setCompanyType(companyType);
        companyMapperExt.updateByPrimaryKeySelective(model);
    }

    @Override
    public void updateQjState(Integer id, boolean isSupply) {
        Company companyModel = new Company();
        companyModel.setCompanyId(id);
        // 更新对应审核标示
        if (isSupply) {
            companyModel.setQjSupplyState(1);
        } else {
            companyModel.setQjAgentState(1);
        }
        companyMapperExt.updateByPrimaryKeySelective(companyModel);
    }

    @Override
    public void updateAgentTime(Integer id) {
        companyMapperExt.updateAgentTime(id);
    }

    @Override
    public void updateShowRoomStateById(Integer id, Integer showRoomState) {
        companyMapperExt.updateShowRoomStateById(id, showRoomState);
    }

    @Override
    public Company getById(Integer id) {
        return companyMapperExt.selectByPrimaryKey(id);
    }

    @Override
    public Optional<Company2Dto> get(Integer id) {
        return Optional.ofNullable(get2(id));
    }

    @Override
    public Company2Dto get2(Integer id) {
        Company2DtoCacheModel cacheModel = new Company2DtoCacheModel(redisUtil, id, companyPublicMapper);
        return cacheModel.getOrLoad();
    }

    @Override
    public List<CompanyDto> selectList(String keyword, String label) {
        return companyPublicMapper.selectList(SqlUtil.like(keyword), label);
    }

    @Override
    public List<CompanyDto> selectTicketCompanyList(String keyword) {
        return companyPublicMapper.selectTicketCompanyList(SqlUtil.like(keyword), LABEL_SUPPLIER);
    }

    @Override
    public boolean isOneWechat(Integer companyId) {
        Company company = getById(companyId);
        return Objects.nonNull(company) && company.getIsOneWechat() == 1;
    }

    @Override
    public CompanyServiceDto getService(Integer companyId) {
        return companyMapperExt.selectService(companyId);
    }

    @Override
    public boolean hasZkgx(Integer companyId) {
        return isByLabel(companyId, LABEL_ZKGX);
    }

    @Override
    public Integer getLabelId(String labelName) {
        return customerLabelMapperExt.selectLabelByName(labelName);
    }

    @Override
    public void reloadCompany(Integer companyId) {
        Company2DtoCacheModel cacheModel = new Company2DtoCacheModel(redisUtil, companyId, companyPublicMapper);
        cacheModel.reload();
    }

    @Override
    public boolean isByLabel(Integer companyId, String labelName) {
        return customerLabelMapperExt.selectByName(LabelApi.LABEL_TYPE_COMPANY, companyId, labelName) > 0;
    }

    @Override
    public boolean isEffective(Integer companyId) {
        CompanyServiceDto companyServiceDto = getService(companyId);
        return DateUtil.uitl(companyServiceDto.getEndTime()) > 0;
    }

    @Override
    public boolean isAudit(Integer companyId) {
        Company company = companyMapperExt.selectByPrimaryKey(companyId);
        if (company.getCreateTime().before(DATE_NEW_USER)) return true;

        Integer status = companyMapperExt.selectLicense(companyId);
        return status == 1;
    }

    @Override
    public boolean isPhoneBind(Integer userId) {
        UserDto userDto = userAdminService.get(userId);
        return Objects.nonNull(userDto) && Objects.nonNull(userDto.getPhone());
    }

    private class Company2DtoCacheModel extends PublicBaseCacheModel<Company2Dto> {
        private CompanyPublicMapper companyPublicMapper;
        private Integer companyId;

        Company2DtoCacheModel(RedisUtil redisUtil, Integer companyId, CompanyPublicMapper companyPublicMapper) {
            super(redisUtil, "company@" + companyId, 3600);//3600=60*60s
            this.companyId = companyId;
            this.companyPublicMapper = companyPublicMapper;
        }

        @Override
        protected Company2Dto getData() {
            return companyPublicMapper.selectById(companyId);
        }
    }
}
